package cn.cvs.dao;

import cn.cvs.pojo.SysStorage;
import cn.cvs.pojo.t_supplier;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class test {
    @Test
    public void list() throws IOException {
        //读取mybatis核心配置文件
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        //创建 SqlSessionFactoryBuilder 构建者
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        //创建SqlSessionFactory对象
        SqlSessionFactory factory= builder.build(is);
        //创建SqlSession对象
        SqlSession sqlSession =factory.openSession();
        //获取SysUserMapper对象
        SysStoraMapper mapper=sqlSession.getMapper(SysStoraMapper.class);
        //调用方法执行SQL
        List<SysStorage> list=mapper.list("红",2,2);
        for (SysStorage sysStorage : list) {
            System.out.println("入库记录编码"+sysStorage.getSrCode()+"\t商品名称"+sysStorage.getGoodsName()+"\t供货商ID"+sysStorage.getSupplierId()+"\t商品总额"+sysStorage.getTotalAmount()+"\t支付状态"+sysStorage.getPayStatus()+"\t创建时间"+sysStorage.getCreatedTime());
        }
    }
    @Test
    public void getStorageRecordByParams2() {
        try {
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建 SqlSessionFactoryBuilder 构建者
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //创建SqlSessionFactory对象
            SqlSessionFactory factory= builder.build(is);
            //创建SqlSession对象
            SqlSession sqlSession =factory.openSession();
            //获取SysUserMapper对象
            SysStoraMapper mapper=sqlSession.getMapper(SysStoraMapper.class);

            List<SysStorage> byParams2 = mapper.getStorageRecordByParams2("", 14, 2);
            for (SysStorage item : byParams2) {
                System.out.println(String.format("入库记录编码：%s\n商品名称：%s\n供货商ID：%s\n商品总额：%s\n支付状态：%s\n供货商名称：%s\n----\n",
                        item.getSrCode(),item.getGoodsName(),item.getSupplierId(),item.getTotalAmount(),item.getPayStatus(),item.getSupplierName()));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void getStorageRecordByParams3(){
        try {
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建 SqlSessionFactoryBuilder 构建者
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //创建SqlSessionFactory对象
            SqlSessionFactory factory= builder.build(is);
            //创建SqlSession对象
            SqlSession sqlSession =factory.openSession();
            //获取SysUserMapper对象
            SysStoraMapper mapper=sqlSession.getMapper(SysStoraMapper.class);
            List<SysStorage> byParams2 = mapper.getStorageRecordByParams3("红", 2, 2);

            for (SysStorage item : byParams2) {
                System.out.println(String.format("入库记录编码：%s\n商品名称：%s\n供货商ID：%s\n商品总额：%s\n支付状态：%s\n供货商名称：%s\n联系人：%s\n联系电话：%s\n----\n",
                        item.getSrCode(),item.getGoodsName(),item.getSupplierId(),item.getTotalAmount(),item.getPayStatus(),item.getSupplierName(),item.gettSupplier().getSupContact(),item.gettSupplier().getSupPhone()));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void gett_supplier(){
        try {
            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建 SqlSessionFactoryBuilder 构建者
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //创建SqlSessionFactory对象
            SqlSessionFactory factory= builder.build(is);
            //创建SqlSession对象
            SqlSession sqlSession =factory.openSession();
            //获取SysUserMapper对象
            SysStoraMapper mapper=sqlSession.getMapper(SysStoraMapper.class);
            List<t_supplier> byParams2 = mapper.gett_supplier(2);

            for (t_supplier item : byParams2) {
                System.out.println("供货商ID"+item.getId()+"供货商编码"+item.getSupCode()+"供货商名称"+item.getSupName()+"联系人"+item.getSupContact()+"联系电话"+item.getSupPhone());
                for (SysStorage sysStorage : item.getSysStorages()) {
                    System.out.println("入库记录编码"+sysStorage.getSrCode()+"商品名称"+ sysStorage.getGoodsName()+"商品总额"+sysStorage.getTotalAmount()+"支付状态"+ sysStorage.getPayStatus());
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * ch02 实战任务5 删除
     */
    @Test
    public void removeSupplier() throws IOException {
        int count = 0;

            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建 SqlSessionFactoryBuilder 构建者
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //创建SqlSessionFactory对象
            SqlSessionFactory factory= builder.build(is);
            //创建SqlSession对象
            SqlSession sqlSession =factory.openSession();
            //获取SysUserMapper对象
            SysStoraMapper mapper=sqlSession.getMapper(SysStoraMapper.class);

            count = mapper.removeSupplier(2);
            sqlSession.commit();
            if (count > 0) {
                System.out.println("删除成功！");
            }else {
                System.out.println("删除失败！");
            }
        }
        
    /**
     * 实战练习5 更新供货商
     */
    @Test
    public void updateSupplier() throws IOException {
        int count = 0;

            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建 SqlSessionFactoryBuilder 构建者
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //创建SqlSessionFactory对象
            SqlSessionFactory factory= builder.build(is);
            //创建SqlSession对象
            SqlSession sqlSession =factory.openSession();
            //获取SysUserMapper对象
            SysStoraMapper mapper=sqlSession.getMapper(SysStoraMapper.class);

            t_supplier t_supplier = new t_supplier();
            t_supplier.setId(16);
            t_supplier.setSupCode("lx_PC12438");
            t_supplier.setSupName("湖南郴州Zero-X有限公司");
            t_supplier.setSupDesc("服务科技");
            t_supplier.setSupContact("李湘");
            t_supplier.setSupPhone("15211704291");
            t_supplier.setSupAddress("湖南省郴州苏仙区郴江路28号");
            t_supplier.setSupFax("0735-98745632111111");
            t_supplier.setUpdatedUserId(1);
            t_supplier.setUpdatedTime(new Date());
            count = mapper.updateSupplier(t_supplier);
            sqlSession.commit();
            if (count > 0) {
                System.out.println("更新成功！！！");
            }else {
                System.out.println("更新失败！！！");
            }
        }

    /**
     * 实战任务5 新增供货商
     */
    @Test
    public void insertSupplier() throws IOException {

        int count = 0;

            InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
            //创建 SqlSessionFactoryBuilder 构建者
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            //创建SqlSessionFactory对象
            SqlSessionFactory factory= builder.build(is);
            //创建SqlSession对象
            SqlSession sqlSession =factory.openSession();
            //获取SysUserMapper对象
            SysStoraMapper mapper=sqlSession.getMapper(SysStoraMapper.class);

            t_supplier t_supplier = new t_supplier();
            t_supplier.setSupCode("lx_PC12438");
            t_supplier.setSupName("湖南郴州Zero-X有限公司");
            t_supplier.setSupDesc("服务科技");
            t_supplier.setSupContact("李湘");
            t_supplier.setSupPhone("15211704292");
            t_supplier.setSupAddress("湖南省郴州苏仙区郴江路29号");
            t_supplier.setSupFax("0735-98745632111111");
            t_supplier.setCreatedUserId(1);
            t_supplier.setCreatedTime(new Date());
            count = mapper.addSupplier(t_supplier);
            sqlSession.commit();
            if (count > 0) {
                System.out.println("新增成功！");
            }else {
                System.out.println("新增失败！");
            }

        }
        }

